package com.supermap.guangzhou.weifang.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.supermap.guangzhou.weifang.R;
import com.supermap.guangzhou.weifang.adapter.MyPageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 导航页
 */
public class GuideActivity extends AppCompatActivity {

    @BindView(R.id.vp_guide)
    ViewPager vpGuide;


    List<View> list = new ArrayList<>();
    @BindView(R.id.ll_dot_group)
    LinearLayout llDotGroup;
    @BindView(R.id.view_red_dot)
    ImageView viewRedDot;
    @BindView(R.id.btn_use)
    Button btnUse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);

        //初始化viwe list
        initViews();

        vpGuide.setAdapter(new MyPageAdapter(list));

        vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) viewRedDot.getLayoutParams();
                lp.setMargins(position * 30, 0, 0, 0);
                viewRedDot.setLayoutParams(lp);
                if(position == list.size()-1){
                    btnUse.setVisibility(View.VISIBLE);
                }else{
                    btnUse.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //初始化指示点个数
        initDotViews();

    }


    public void initDotViews() {
        llDotGroup.removeAllViews();

        for (int i = 0; i < list.size(); i++) {
            ImageView view = new ImageView(this);


            view.setBackgroundResource(R.drawable.shape_dot_gray);
            llDotGroup.addView(view);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 20);
            view.setLayoutParams(lp);
            if (i == 0) {
                lp.setMargins(0, 0, 0, 0);
            } else {
                lp.setMargins(10, 0, 0, 0);
            }
            view.setLayoutParams(lp);

        }

    }

    public void initViews() {
        int[] mipmaps = {R.mipmap.flash1, R.mipmap.flash2, R.mipmap.flash3};

        for (int i : mipmaps) {
            ImageView imageView = new ImageView(this);
            imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), i));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            list.add(imageView);
        }
    }

    @OnClick(R.id.btn_use)
    public void onViewClicked() {
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
        finish();
    }


    /**
     * 沉浸式
     * @param hasFocus
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}
